cmake_minimum_required(VERSION 3.14)
set(PROJECT_NAME "calcengine_flutter_libs")
project(${PROJECT_NAME} LANGUAGES C CXX)

# 设置 calculator 下载链接 11.2509.0
set(CALCULATOR_URL "https://github.com/microsoft/calculator/archive/637391a10855370344fae365b938bf27de80de6f.zip")

# This value is used when generating builds using this plugin, so it must
# not be changed.
set(PLUGIN_NAME "calcengine_flutter_libs_plugin")

include(FetchContent)
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
  FetchContent_Declare(
    calculator
    URL ${CALCULATOR_URL} 
    DOWNLOAD_EXTRACT_TIMESTAMP NEW
  )
else()
  FetchContent_Declare(
    calculator
    URL ${CALCULATOR_URL}
  )
endif()
FetchContent_MakeAvailable(calculator)

file(GLOB_RECURSE SOURCES 
  "${calculator_SOURCE_DIR}/src/CalcManager/CEngine/*.cpp"
  "${calculator_SOURCE_DIR}/src/CalcManager/Ratpack/*.cpp"
  "${calculator_SOURCE_DIR}/src/CalcManager/*.cpp"
)
add_library(${PLUGIN_NAME} SHARED
  "calcengine_flutter_libs_plugin.cc"
  ${SOURCES}
)

target_compile_features(${PLUGIN_NAME} PUBLIC cxx_std_17)
target_compile_options(${PLUGIN_NAME} PRIVATE 
  "$<$<NOT:$<CONFIG:Debug>>:-O3>"
  -Wno-dangling-capture
  -Wno-switch
  -Wno-unqualified-std-cast-call
)

set_target_properties(${PLUGIN_NAME} PROPERTIES
  CXX_VISIBILITY_PRESET hidden)
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)

target_include_directories(${PLUGIN_NAME} INTERFACE
  "${CMAKE_CURRENT_SOURCE_DIR}/include"
)

target_include_directories(${PLUGIN_NAME} PRIVATE
  "${calculator_SOURCE_DIR}/src/CalcManager"
  "${calculator_SOURCE_DIR}/src/CalcManager/Ratpack"
  "${calculator_SOURCE_DIR}/src/CalcManager/CEngine"
  "${calculator_SOURCE_DIR}/src/CalcManager/Header Files"
)

set(PCH_FILE ${calculator_SOURCE_DIR}/src/CalcManager/pch.h)
if(EXISTS ${PCH_FILE})
  file(READ ${PCH_FILE} PCH_CONTENT)
    
  string(REPLACE "#include <intsafe.h>" "// #include <intsafe.h>" PCH_CONTENT "${PCH_CONTENT}")
  string(REPLACE "#include <winerror.h>" "// #include <winerror.h>" PCH_CONTENT "${PCH_CONTENT}")
    
  file(WRITE ${PCH_FILE} "${PCH_CONTENT}")
  message(STATUS "Modified pch.h to comment out Windows-specific includes")
  target_precompile_headers(${PLUGIN_NAME} PRIVATE
    "${calculator_SOURCE_DIR}/src/CalcManager/pch.h"
  )
endif()

target_link_libraries(${PLUGIN_NAME} PRIVATE flutter)
